package com.example.myandroidapp1;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.example.myandroidapp1.activity.AbstractBaseActivity;

public class Tabs extends AbstractBaseActivity implements OnClickListener {

	private TabHost th;
	private long start, stop;
	private TextView showResults;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tabs);
		th = (TabHost) findViewById(android.R.id.tabhost);

		Button newTab = (Button) findViewById(R.id.bAddTab);
		Button bStart = (Button) findViewById(R.id.bStartWatch);
		Button bStop = (Button) findViewById(R.id.bStopWatch);

		showResults = (TextView) findViewById(R.id.tvResults);

		newTab.setOnClickListener(this);
		bStart.setOnClickListener(this);
		bStop.setOnClickListener(this);

		th.setup();

		TabSpec spec = th.newTabSpec("tag1");
		spec.setContent(R.id.tab1);
		spec.setIndicator("StopWatch");
		th.addTab(spec);

		spec = th.newTabSpec("tag2");
		spec.setContent(R.id.tab2);
		spec.setIndicator("Tab 2");
		th.addTab(spec);

		spec = th.newTabSpec("tag3");
		spec.setContent(R.id.tab3);
		spec.setIndicator("Add a tab");
		th.addTab(spec);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.bStartWatch:
			start = System.currentTimeMillis();

			break;

		case R.id.bStopWatch:
			stop = System.currentTimeMillis();
			if(start < stop){
				long res = stop - start;
				int millis = (int) res;
				int seconds =(int) res/1000;
				int minutes = seconds/60;
				int hours = minutes/60;
				
				millis = millis % 1000;
				seconds = seconds % 60;
				minutes = minutes % 60;
				showResults.setText(String.format(" %d : %02d : %02d : %03d ", hours,minutes,seconds,millis));
			}
			break;
		case R.id.bAddTab:

			TabSpec ourSpec = th.newTabSpec("tag 3");
			ourSpec.setContent(new TabHost.TabContentFactory() {

				@Override
				public View createTabContent(String tag) {
					// TODO Auto-generated method stub
					TextView textView = new TextView(Tabs.this);
					textView.setText("You've created a new tab!!!");
					return textView;
				}
			});
			ourSpec.setIndicator("New Tab");
			th.addTab(ourSpec);
			break;
		default:
			break;
		}
	}
}
